(function($) {

    var corpo       = '#main';  // div corpo ajax
    caracter        = '#!/';    // url caracter ajax
    var controller  = 'home';   // controller inicial
    var action      = 'index';  // action inicial
    var ids         = '';
    var base        = 'index/index';
    json            = false;


    var app = $.sammy(corpo, function() {

        this.helpers({
            efeito_corpo: function(tipo) {
                this.swap = function(content) {
                    switch(tipo) {
                        case 0:
                            this.$element().html(content);
                            break;
                        case 1:
                            this.$element().hide().html(content).fadeIn(1500);
                            break;
                        case 2:
                            this.$element().hide().html(content).show("highlight", {
                                }, 1500);
                            break;
                    }
                    init_function();
                }
            },

            get_ajax: function(url, params) {
                $( "#dialog" ).dialog( "open" );
                this.efeito_corpo(1);
                this.partial(url, params);
                
            },

            post_ajax: function(ctx) {
                $( "#dialog" ).dialog( "open" );
                $.ajax({
                    type: 'POST',
                    url: ctx.params.controller + "/" + ctx.params.action,
                    data: ctx.params.toHash(),
                    success: function(retorno){
                        json = $.parseJSON(retorno);
                        app.clearTemplateCache();
                        
                        if (json.ok != 'true') return ctx.redirect(caracter);
                        
                        return ctx.redirect(caracter + json.action + "/param1/" + json.param1 + "/param2/" + json.param2);
                    }
                });
            },

            post_ajax_teste: function(ctx) {
                $( "#dialog" ).dialog( "open" );
                $.ajax({
                    type: 'POST',
                    url: ctx.params.controller + "/" + ctx.params.action,
                    data: ctx.params.toHash(),
                    success: function(retorno){
                        json = $.parseJSON(retorno);
                        if (json.ok != 'true') return ctx.redirect(caracter);
                        //app.clearTemplateCache();
                        //app.refresh();
                        return ctx.redirect(caracter + json.action + "/param1/" + json.param1 + "/param2/" + json.param2);
                    }
                });
            }

        });
        
        // GET #!/index/index
        this.get(caracter + base, function() {
            this.redirect(caracter);
        });
        
        // GET #!/
        this.get(caracter, function() {
            this.get_ajax(controller + "/" + action);
        });

        // GET #!/:action
        this.get(caracter + ':action', function() {
            this.get_ajax(controller + "/" + this.params.action);
        });
        
        // GET #!/:action/param1/:value1/param2/:value2
        this.get(caracter + ':action/param1/:value1/param2/:value2', function() {
            if (json) {
                json = false;
                this.get_ajax(controller + "/" + this.params.action + "/param1/" + this.params.value1 + "/param2/" + this.params.value2);
            } else {
                this.redirect(caracter);
            }
        });
        
        // POST #!/:action
        this.post(caracter + ':action', function(ctx) {
            ctx.params.controller = controller
            
            this.post_ajax(ctx);
        });
        
    });

    // inicializa o sammyjs
    $(function() {
        app.run(caracter);
        transformar_url(ids);
    });
    
    function init_function() {
        transformar_url(corpo);
        $( "#dialog" ).dialog( "close" );
        $(':text:first').focus();
        if ($(':text:first').length == 0) {
            $(':button:first').focus();
        }
    }
    
    function is_controller_index() {
        return (app.getLocation().indexOf("/index") != -1) ? true : false;
    }
    
    function transformar_url(div) {
        if (!div) return false;
        $("a[ajax='\u2713'], form[ajax='\u2713']", div).each( function(){
            var href_or_action = $(this).is('a') ? 'href' : 'action';
            var url = $(this).attr(href_or_action);
            if(url && !(url.indexOf(caracter) != -1)) {
                var v = url.split("/");
                v.shift();
                if(v[0] == controller && v[1] == action)  $(this).attr(href_or_action,caracter);
                else if(v[0] == controller) {
                    v.shift();
                    $(this).attr(href_or_action,caracter + v.join("/"));
                }
                else $(this).attr(href_or_action,caracter + v.join("/"));
            }
        });
        return false;
    }

})(jQuery);